package com.css.order.service.impl;

import com.css.order.VO.ResultVO;
import com.css.order.client.ProductClient;
import com.css.order.dataobject.OrderDetail;
import com.css.order.dataobject.OrderMaster;
import com.css.order.dataobject.ProductInfo;
import com.css.order.dto.CartDTO;
import com.css.order.dto.OrderDTO;
import com.css.order.enums.OrderStatusEnum;
import com.css.order.enums.PayStatusEnum;
import com.css.order.respository.OrderDetailRepository;
import com.css.order.respository.OrderMasterRepository;
import com.css.order.service.OrderService;
import com.css.order.util.KeyUtil;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * -
 * </p>
 *
 * @author css
 * @since 2019/12/27
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDetailRepository detailRepository;
    @Autowired
    private OrderMasterRepository masterRepository;

    @Autowired
    private ProductClient productClient;

    /**
     * 创建订单
     *
     * @param orderDTO
     * @return
     */
    @Override
    public OrderDTO create(OrderDTO orderDTO) {
        String orderId = KeyUtil.genUniqueKey();
        List<String> collect = orderDTO.getOrderDetailList().stream()
                .map(OrderDetail::getProductId)
                .collect(Collectors.toList());
        List<ProductInfo> productInfos = productClient.listForOrder(collect);
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) {
            for (ProductInfo productInfo : productInfos) {
                if (productInfo.getProductId().equals(orderDetail.getProductId())) {
                    totalPrice = productInfo.getProductPrice().multiply(new BigDecimal(orderDetail.getProductQuantity()))
                            .add(totalPrice);
                    BeanUtils.copyProperties(productInfo, orderDetail);
                    orderDetail.setOrderId(orderId);
                    orderDetail.setDetailId(KeyUtil.genUniqueKey());
                }
            }
        }

        List<CartDTO> cartDTOS = orderDTO.getOrderDetailList().stream()
                .map(e -> new CartDTO(e.getProductId(), e.getProductQuantity()))
                .collect(Collectors.toList());
        productClient.decreaseStock(cartDTOS);


        OrderMaster orderMaster = new OrderMaster();
        orderDTO.setOrderId(orderId);
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMaster.setOrderAmount(totalPrice);
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
        orderMaster.setCreateTime(LocalDate.now());
        orderMaster.setUpdateTime(LocalDate.now());
        masterRepository.save(orderMaster);
        return orderDTO;
    }
}
